package com.fortune.rainshine.exhandle;

import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.enums.ExceptionEnum;
import com.fortune.rainshinecommon.enums.ResultCode;
import com.fortune.rainshinecommon.exceptions.HeaderException;
import com.fortune.rainshinecommon.exceptions.SignException;
import com.fortune.rainshinecommon.exceptions.TokenException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutionException;


/**
 * @author wcy
 * @description 统一异常处理器
 * @createDate 18-3-28
 * @description
 * @createDate 2018/4/24
 * @updateBy
 * @updateDate
 */
@ControllerAdvice
public class AppExceptionHandler {
    private final static Logger logger= LoggerFactory.getLogger(AppExceptionHandler.class);
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
//    public MethodException handle(Exception e){
    public MsgModel handle(Exception e){
//        String data=e.getMessage();
//        Map<String,Object> map=new HashMap<>();
        if(e instanceof SQLException){
            logger.error("[抱歉,系统异常了] {}", e);
//            return MsgModel.throwException(ExceptionEnum.SQLERROR);
            return new MsgModel(ExceptionEnum.SQLERROR.getErrMsg(),ResultCode.FAILURE);
        }
        else if(e instanceof MethodArgumentNotValidException){
            //重写MethodArgumentNotValidException拼接message迭代器
            BindingResult result = ((MethodArgumentNotValidException) e).getBindingResult();
            Iterator var2 = result.getAllErrors().iterator();
            List<String> errorList = new ArrayList<>();
            while(var2.hasNext()) {
                ObjectError error = (ObjectError)var2.next();
                errorList.add(error.getDefaultMessage());
            }
            logger.error("[抱歉,系统异常了] {}",ExceptionEnum.VALIDATIONERROR.getErrMsg()+": "+errorList);
//            return MsgModel.throwJson(103,errorList);
            return new MsgModel(ExceptionEnum.VALIDATIONERROR.getErrMsg(),ResultCode.FAILURE);
        }
        else if(e instanceof TokenException){
            logger.error("[抱歉,系统异常了] {}", e);
//            return MsgModel.throwException(ExceptionEnum.TOKENERROR);
            return new MsgModel(ExceptionEnum.TOKENERROR.getErrMsg(),ResultCode.FAILURE);
        }
        else if(e instanceof SignException){
            logger.error("[抱歉,系统异常了] {}", e);
            return new MsgModel(ExceptionEnum.SIGNERROR.getErrMsg(),ResultCode.FAILURE);
//            return MsgModel.throwException(ExceptionEnum.SIGNERROR);
        }
        else if(e instanceof HeaderException){
            logger.error("[抱歉,系统异常了] {}", e);
//            return MsgModel.throwException(ExceptionEnum.MISSHEADER);
            return new MsgModel(ExceptionEnum.MISSHEADER.getErrMsg(),ResultCode.FAILURE);
        }
        else if(e instanceof InterruptedException){
            logger.error("[抱歉,系统异常了] {}", e);
//            return MsgModel.throwException(ExceptionEnum.INTERRUPTED);
            return new MsgModel(ExceptionEnum.INTERRUPTED.getErrMsg(),ResultCode.FAILURE);
        }
        else if(e instanceof ExecutionException){
            logger.error("[抱歉,系统异常了] {}", e);
//            return MsgModel.throwException(ExceptionEnum.EXCUTION);
            return new MsgModel(ExceptionEnum.EXCUTION.getErrMsg(),ResultCode.FAILURE);
        }
        else{
            logger.error("[抱歉,系统异常了] {}", e);
//            return MsgModel.throwException(ExceptionEnum.NUKNOWERROR);
            return new MsgModel(ExceptionEnum.NUKNOWERROR.getErrMsg(),ResultCode.FAILURE);
        }
    }
}
